﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BAL;
using Common;
using System.Collections.Generic;
namespace Web
{
    
    public partial class ExamPaperAutoCreate : System.Web.UI.Page
    {
        IList<TestPaperPolicySubInfo> subs = null;
        protected int SubjectId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetUrlParams();
                subs = GetSubPolicyInfo();
                if (subs == null)
                {
                    hInfo.InnerHtml = "请先选择一个策略";
                }
                else
                {
                    if (subs.Count() > 0)
                    {
                        hInfo.InnerHtml = "正在生成试卷........";
                        CreateElectron();
                    }
                    else
                    {
                        hInfo.InnerHtml = "请先创建一个子策略";
                    }
                }
            }
        }
        bool CreateElectron()
        {

            IList<QuestionInfo> mainList = new List<QuestionInfo>();
            IList<Paper_QueInfo> listAdd = new List<Paper_QueInfo>();
            int i = 1;
            foreach (TestPaperPolicySubInfo subItem in subs)
            {
                BaseElectronTestPagerStratory stratory = new ElectronTestPagerStratory();
                stratory.SubjectId = SubjectId;
                XmlQuestion xmlQuestion = XmlQuestionHelper.GetXmlQuestionInfo(subItem.Content);
 
                IList<QuestionInfo> list = stratory.GenerateElectronTestPager(subItem.Content, mainList.Where(p => p.QuesTypeId.ToString() == xmlQuestion.Type).ToList<QuestionInfo>());
                if (list == null)
                {
                    hInfo.InnerHtml = subItem.Name+"-->没有找到合适的数据";
                    return false;
                }
                if (list.Count() == 0)
                {
                    hInfo.InnerHtml = subItem.Name + "-->没有找到合适的数据";
                    return false;
                }
                i = 1;
                foreach (var item in list)
                {
                    listAdd.Add(new Paper_QueInfo { CreateDate = DateTime.Now, ExamPaperId = int.Parse(this.ExamPaperId), QuesId = item.Id, OrderId = i, SubPolicyId =subItem.Id });
                    i++;
                }
                mainList = mainList.Concat(list).ToList<QuestionInfo>();
            }
            if (mainList.Count() == 0)
            {
                hInfo.InnerHtml = "没有找到合适的数据";
                return false;
            }
            else
            {
                try
                {
                    if (ExamPaperId != "" && ExamPaperId != "0")
                    {
                        Paper_Ques.DeletePaper_QuesInfoByExamPagerId(int.Parse(this.ExamPaperId));
                       
                        
                        BAL.Paper_Ques.Add(listAdd);
                    }
                }
                catch
                {
                }
                hInfo.InnerHtml = "生成成功";
            }

            return true;
        }
        private IList<TestPaperPolicySubInfo> GetSubPolicyInfo()
        {
            if (PolicyMainId != "" && PolicyMainId!="0")
            {
                IList<TestPaperPolicySubInfo> subs = TestPaperPolicySub.GetList(int.Parse(PolicyMainId));
                if (subs == null)
                    return null;
                return subs;
            }
            return null;
        }
        void GetUrlParams()
        {
            if (this.Request["id"] != null && this.Request["id"].ToString() != "")
            {
                this.ExamPaperId = this.Request["id"].ToString();
            }
            if (this.Request["policyid"] != null && this.Request["policyid"].ToString() != "")
            {
                this.PolicyMainId = this.Request["policyid"].ToString();
            }
            if (this.Request["SubjectId"] != null && this.Request["SubjectId"].ToString() != "")
            {
                this.SubjectId = int.Parse(this.Request["SubjectId"].ToString());
            }
        }
        public string PolicyMainId
        {
            get
            {
                if (this.ViewState["PolicyMainId"] != null)
                {
                    return this.ViewState["PolicyMainId"].ToString();
                }
                else
                {
                    return "";
                }
            }
            set
            {
                this.ViewState["PolicyMainId"] = value;
            }
        }

        public string ExamPaperId
        {
            get
            {
                if (this.ViewState["ExamPaperId"] == null)
                {
                    return "";
                }
                else
                {
                    return this.ViewState["ExamPaperId"].ToString();
                }
            }
            set
            {
                this.ViewState["ExamPaperId"] = value;
            }
        }
    }
}
